﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace Model.Entities
{
    public class ModuleCollection
    {
        //Load all module setting from xml and store in database and cache
        //add new module function in BO
        private Dictionary<string, Module> dict;

        public List<Module> Modules { get; set; }

        public ModuleCollection()
        {
            Modules = new List<Module>();
            dict = new Dictionary<string, Module>();
        }

        public Module this[string idx]
        {
            get
            {
                return dict.ContainsKey(idx) ? dict[idx] : null;
            }
        }

        public static ModuleCollection Import(string xmlFile)
        {
            //import xml setting file [ModuleSettings/PortalSetting.xml]
            using (Stream stream = File.OpenRead(xmlFile))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ModuleCollection));
                    var tmp = (ModuleCollection)serializer.Deserialize(stream);
                    return tmp;
                }
                finally
                {
                    stream.Close();
                }
            }
        }

        public static bool Add(Module module)
        {
            //add new module to database and store in cache again
            return true;
        }
    }
}
